using System.Collections.Generic;
using IQIGame.Onigao.Logic;
using UnityEngine.Playables;

namespace IQIGame.Onigao.GamePlay
{
    public class BattleHideBehaviour : PlayableBehaviour
    {
        public BattleHideClip.EBattleHideType exceptType;
        public int effectiveIndex;
        public bool onlyWidget;

        public EntityDriver entityDriver;

        public List<UnitView> hides = new List<UnitView>();

        public override void OnBehaviourPlay(Playable playable, FrameData info)
        {
            base.OnBehaviourPlay(playable, info);
            if (this.hides.Count > 0)
            {
                this.Reset();
            }
            this.Hide();
        }

        public override void OnBehaviourPause(Playable playable, FrameData info)
        {
            base.OnBehaviourPause(playable, info);
            this.Reset();
        }

        private void Hide()
        {
#if UNITY_EDITOR
            if (BattleClientUtil.IsEditorMode)
            {
                return;
            }
#endif
            if (!this.entityDriver)
            {
                return;
            }
            var rUnitList = BattleManager.Instance.ctrl.data.units;
            BattleManager.Instance.ctrl.data.TryGetUnitByDriver(this.entityDriver, out var rSelf);
            if (rSelf == null)
            {
                return;
            }
            for (int i = 0; i < rUnitList.Count; i++)
            {
                var rUnit = rUnitList[i];
                if (rSelf == rUnit && (this.exceptType & BattleHideClip.EBattleHideType.Self) > 0)
                {
                    continue;
                }
                var rTeamRelation = BattleUtil.CheckTeamRelation(rSelf.teamIndex, rUnit.teamIndex);
                if (((this.exceptType & BattleHideClip.EBattleHideType.Enemy) > 0 && rTeamRelation == ETargetRelation.Enemy) ||
                    ((this.exceptType & BattleHideClip.EBattleHideType.Friend) > 0 && rTeamRelation == ETargetRelation.Friend))
                {
                    continue;
                }
                var rTargets = rSelf.GetComponent<BattleTimelineComponent>(ETComponentType.BattleTimelineComponent).GetCurTarget(this.effectiveIndex - 1);
                bool bIsTarget = rTargets.Contains(rUnit.GUID);
                if ((this.exceptType & BattleHideClip.EBattleHideType.Target) > 0 && bIsTarget)
                {
                    continue;
                }
                this.hides.Add(rUnit);
            }
            BattleManager.Instance.ctrl.hideCtrl.AddUnit(this.hides, this.onlyWidget);
            if ((this.exceptType & BattleHideClip.EBattleHideType.Scene) == 0)
            {
                BattleManager.Instance.ctrl.hideCtrl.Add(0);
            }
            if ((this.exceptType & BattleHideClip.EBattleHideType.UI) == 0)
            {
                BattleManager.Instance.ctrl.hideCtrl.Add(1);
            }
        }

        private void Reset()
        {
#if UNITY_EDITOR
            if (BattleClientUtil.IsEditorMode)
            {
                return;
            }
#endif
            BattleManager.Instance.ctrl.hideCtrl.RemoveUnit(this.hides, this.onlyWidget);
            this.hides.Clear();

            if ((this.exceptType & BattleHideClip.EBattleHideType.Scene) == 0)
            {
                BattleManager.Instance.ctrl.hideCtrl.Remove(0);
            }
            if ((this.exceptType & BattleHideClip.EBattleHideType.UI) == 0)
            {
                BattleManager.Instance.ctrl.hideCtrl.Remove(1);
            }
        }
    }
}